package cn.cloud.all.security.oauth2.common.util;

import org.codehaus.jackson.map.ObjectMapper;

import java.util.Map;

public class JacksonJsonParser implements JsonParser {

    private ObjectMapper mapper = new ObjectMapper();

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> parseMap(String json) {
        try {
            return mapper.readValue(json, Map.class);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot parse json", e);
        }
    }

    @Override
    public String formatMap(Map<String, ?> map) {
        try {
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot format json", e);
        }
    }
}
